package com.ruoyi.project.system.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.system.domain.SysGoods;
import com.ruoyi.project.system.mapper.SysGoodsMapper;
import com.ruoyi.project.system.service.ISysGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商品列表Service业务层处理
 *
 * @author orange
 * @date 2022-04-09
 */
@Service
public class SysGoodsServiceImpl implements ISysGoodsService {
  @Autowired private SysGoodsMapper sysGoodsMapper;

  /**
   * 查询商品列表
   *
   * @param goodsId 商品列表主键
   * @return 商品列表
   */
  @Override
  public SysGoods selectSysGoodsByGoodsId(Long goodsId) {
    return sysGoodsMapper.selectSysGoodsByGoodsId(goodsId);
  }

  /**
   * 查询商品列表列表
   *
   * @param sysGoods 商品列表
   * @return 商品列表
   */
  @Override
  public List<SysGoods> selectSysGoodsList(SysGoods sysGoods) {
    return sysGoodsMapper.selectSysGoodsList(sysGoods);
  }

  /**
   * 新增商品列表
   *
   * @param sysGoods 商品列表
   * @return 结果
   */
  @Override
  public int insertSysGoods(SysGoods sysGoods) {
    sysGoods.setCreateTime(DateUtils.getNowDate());
    return sysGoodsMapper.insertSysGoods(sysGoods);
  }

  /**
   * 修改商品列表
   *
   * @param sysGoods 商品列表
   * @return 结果
   */
  @Override
  public int updateSysGoods(SysGoods sysGoods) {
    sysGoods.setUpdateTime(DateUtils.getNowDate());
    return sysGoodsMapper.updateSysGoods(sysGoods);
  }

  /**
   * 批量删除商品列表
   *
   * @param goodsIds 需要删除的商品列表主键
   * @return 结果
   */
  @Override
  public int deleteSysGoodsByGoodsIds(Long[] goodsIds) {
    return sysGoodsMapper.deleteSysGoodsByGoodsIds(goodsIds);
  }

  /**
   * 删除商品列表信息
   *
   * @param goodsId 商品列表主键
   * @return 结果
   */
  @Override
  public int deleteSysGoodsByGoodsId(Long goodsId) {
    return sysGoodsMapper.deleteSysGoodsByGoodsId(goodsId);
  }

  @Override
  public int reduceStore(Long goodsId, Long num) {

    SysGoods sysGoods = sysGoodsMapper.selectSysGoodsByGoodsId(goodsId);
    if (sysGoods.getQuantity() < num) {
      throw new ServiceException("库存数量不足");
    }

    return sysGoodsMapper.reduceStore(goodsId, num);
  }
}
